package com.example.v3admin.controller;



import com.example.v3admin.common.result.BusinessException;
import com.example.v3admin.common.result.Result;
import com.example.v3admin.model.SysUser;
import com.example.v3admin.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户接口")
@RestController
@RequestMapping("/users")
@Slf4j
public class UserController {


    @Autowired
    private SysUserService userService;

    /**
    *  获取用户列表
    */
    @Operation(summary = "获取用户列表")
    @GetMapping
    public List<SysUser> listUsers(){

        log.info("==================== 获取用户列表秦秋 ============================");
        log.debug("==================== ddddd ============================");
        return userService.list();
    }

    /**
     *  获取用户详情
     */
    @Operation(summary = "获取用户详情")
    @GetMapping("/{id}")
    public Result<SysUser> getUserById(
            @Parameter(description = "用户ID")  @PathVariable Long id
    ){

        SysUser user = userService.getById(id);

        if(user==null){
          throw new BusinessException("用户不存在");
        }
        return Result.success(user);
    }

    /**
     *  新增用户
     */
    @Operation(summary = "新增用户")
    @PostMapping
    public String createUser(@RequestBody SysUser user){
        userService.save(user);
        return "用户创建成功";
    }

    /**
     *  更新用户信息
     */
    @Operation(summary = "更新用户信息")
    @PutMapping("/{id}")
    public String updateUser(
            @Parameter(description = "用户ID") @PathVariable Long id,
            @RequestBody SysUser user
    ){
        userService.updateById(user);
        return "用户创建成功";
    }

    /**
     *  删除用户
     */
    @Operation(summary = "删除用户")
    @DeleteMapping("/{id}")
    public String deleteUser(
            @Parameter(description = "用户ID") @PathVariable Long id
    ){
        userService.removeById(id);
        return "用户创建成功";
    }

}
